// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router/';
import Panel from './components/panel.vue'; // 项目容器组件

import store from './store/store';
import * as types from './store/types';

import * as filters from './utils/filters'; // 全局vue filter

import elementUI from 'element-ui';

import Lookup from '@/components/business/ymyLookup';

import 'expose-loader?httpClient!./utils/httpClient';
import 'expose-loader?_!lodash';

// import 'element-ui/lib/theme-default/index.css';
import 'element-ui/lib/theme-chalk/index.css';
import 'font-awesome/css/font-awesome.min.css';

Vue.config.productionTip = false;
Vue.use(elementUI);
Vue.component('panel', Panel);
Vue.component('ymy-lookup', Lookup);

// 页面刷新时，重新赋值token
if (window.localStorage.getItem('token')) {
  // store.commit(types.LOGIN, window.localStorage.getItem('token'))
}

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (store.state.token) {  // 通过vuex state获取当前的token是否存在
      next();
    }
    else {
      // next({
      //   path: '/login',
      //   query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      // })
      //因为没有登录也，暂默认执行next()
      next();
    }
  }
  else {
    next();
  }
});

//register global utility filters.
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<app/>',
  components: { App }
});
